package com.railway.switchs.action.system;

import com.railway.switchs.domain.dto.user.UserInfo;
import com.railway.switchs.domain.query.business.*;
import com.railway.switchs.domain.query.user.UserInfoQuery;
import com.railway.switchs.enums.EnumNewsType;
import com.railway.switchs.enums.EnumRepairType;
import com.railway.switchs.service.business.*;
import com.railway.switchs.service.user.IUserInfoService;
import com.railway.switchs.utils.StringUtil;
import com.railway.switchs.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

/**
 * @ClassName SystemAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back")
public class SystemAction {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IRoadSwitchService roadSwitchService;

    @Autowired
    private ISwitchInfoService switchInfoService;

    @Autowired
    private IDeviceInfoService deviceInfoService;

    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private IRepairTaskService repairTaskService;


    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = new UserInfo();
        //获取用户信息
        UserInfoQuery query = new UserInfoQuery();
        query.setUserAccount(UserUtil.getUserAccount());
        List<UserInfo> userInfoList = userInfoService.getList(query);
        if(StringUtil.isNotEmpty(userInfoList)){
            userInfo = userInfoList.get(0);
        }
        mav.addObject("userInfo",userInfo);
        mav.addObject("openWarn",false);
        if("administrators".equals(userInfo.getRoleCode())){
            SysNewsQuery sysNewsQuery = new SysNewsQuery();
            sysNewsQuery.setNewsType(EnumNewsType.TO_BE_ANNOUNCED.getCode());
            mav.addObject("sysNewsNum",sysNewsService.getList(sysNewsQuery).size());
            UserInfoQuery userInfoQuery = new UserInfoQuery();
            mav.addObject("userInfoNum",userInfoService.getList(userInfoQuery).size());
        }
        if("repairman".equals(userInfo.getRoleCode())){
            RepairTaskQuery repairTaskQuery = new RepairTaskQuery();
            repairTaskQuery.setRepairmanAccount(userInfo.getUserAccount());
            repairTaskQuery.setTaskType(EnumRepairType.UNHANDLE.getCode());
            mav.addObject("handleRepairTaskNum",repairTaskService.getList(repairTaskQuery).size());
        }
        if("supervisor".equals(userInfo.getRoleCode())){
            RepairTaskQuery repairTaskQuery = new RepairTaskQuery();
            repairTaskQuery.setSupervisorAccount(userInfo.getUserAccount());
            repairTaskQuery.setTaskType(EnumRepairType.UNHANDLE.getCode());
            mav.addObject("repairTaskSum",repairTaskService.getList(repairTaskQuery).size());
        }
        if("earlyWarner".equals(userInfo.getRoleCode())){
            RepairTaskQuery repairTaskQuery = new RepairTaskQuery();
            repairTaskQuery.setTaskType(EnumRepairType.UNQUALIFIED.getCode());
            int countTemp = repairTaskService.getList(repairTaskQuery).size();
            repairTaskQuery.setTaskType(EnumRepairType.OVERTIME.getCode());
            int warnRepairTaskNum = repairTaskService.getList(repairTaskQuery).size() + countTemp;
            mav.addObject("warnRepairTaskNum",warnRepairTaskNum);
            mav.addObject("openWarn",warnRepairTaskNum > 0 ? true : false);
        }
        RoadSwitchQuery roadSwitchQuery = new RoadSwitchQuery();
        mav.addObject("roadSwitchNum",roadSwitchService.getList(roadSwitchQuery).size());
        SwitchInfoQuery switchInfoQuery = new SwitchInfoQuery();
        mav.addObject("switchInfoNum",switchInfoService.getList(switchInfoQuery).size());
        DeviceInfoQuery deviceInfoQuery = new DeviceInfoQuery();
        mav.addObject("deviceInfoNum",deviceInfoService.getList(deviceInfoQuery).size());
        mav.setViewName("system/welcome");
        return mav;
    }

    @RequestMapping("/error")
    public String error(){
        return "system/error";
    }

    @RequestMapping("/unLogin")
    public String unLogin(){
        return "system/unLogin";
    }

    @RequestMapping("/sqlMonitor")
    public String sqlMonitor(){
        return "redirect:/druid/index.html";
    }

}
